package fm.dongman.animefm.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import java.util.List;

import fm.dongman.animefm.R;
import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.view.activity.SingleActivity;
import fm.dongman.animefm.view.transform.GlideRoundTransform;
import fm.dongman.animefm.view.util.ActivityHelper;
import fm.dongman.modellib.model.SheetModel;

import static fm.dongman.animefm.view.activity.SingleActivity.Companion;

/**
 * 发现》专题列表adapter
 * Created by Administrator on 2017/10/15.
 */

public class FSheetSpecialAdapter extends RecyclerView.Adapter<FSheetSpecialAdapter.ViewHolder> {
    private Context mContext;
    private List<IModel.ISheetModel> mListSheet;

    public FSheetSpecialAdapter(List<IModel.ISheetModel> ListSheet, Context context) {
        this.mContext = context;
        this.mListSheet = ListSheet;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_find_anime_sheet_special, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final SheetModel mode = (SheetModel) mListSheet.get(position);
        Glide.with(mContext).load(mode.getMandanImg())
                .transform(new CenterCrop(mContext), new GlideRoundTransform(mContext, 2)).into(holder.mMandanImg);
        holder.mTitle.setText(mode.getMandanTitle());
        holder.mPublishTime.setText(mode.getCreateTime());
        holder.mInfo.setText(mode.getBrowseCount() + " 浏览 | " + mode.getCollectCount() + "收藏");
        holder.mSummary.setText(mode.getSummary());

        if (position == mListSheet.size() - 1)
            holder.mLine.setVisibility(View.GONE);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!(mContext instanceof Activity)) {
                    throw new RuntimeException(mContext.toString() + " 必须是Activity!");
                } else {
                    Intent intent = new Intent(mContext, SingleActivity.class);
                    intent.putExtra(Companion.getEXTRA_NAME(), Companion.getINTENT_EXTRA_SPECIAL_INFO());
                    intent.putExtra(Companion.getBUNDLE_EXTRA_KEY_ID(), mode.getMandanId());
                    ActivityHelper.INSTANCE.startActivity((Activity) mContext, intent);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mListSheet.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView mMandanImg;  //图片
        private TextView mTitle; //标题
        private TextView mPublishTime; //发布时间
        private TextView mInfo; //
        private TextView mSummary;//简介
        private View mLine;

        public ViewHolder(View itemView) {
            super(itemView);
            mMandanImg = (ImageView) itemView.findViewById(R.id.mIvFindSheetPic);
            mTitle = (TextView) itemView.findViewById(R.id.mTvFindSheetTitle);
            mPublishTime = (TextView) itemView.findViewById(R.id.mTvFindSheetPublishTime);
            mInfo = (TextView) itemView.findViewById(R.id.mTvFindSheetOtherInfo);
            mSummary = (TextView) itemView.findViewById(R.id.mTvFindSheetSummary);
            mLine = itemView.findViewById(R.id.mTvFindSheetLine);
        }
    }
}
